/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int res = 0;
    int diameterOfBinaryTree(TreeNode* root) {
        height(root);
        return res;
    }
    int height(TreeNode* root) {
        if(!root) return 0;
        int left = height(root -> left);
        int right = height(root -> right);
        res = max(res, left + right);
        return max(right, left) + 1;
    }
};
